/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.thinktank.service;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.thinktank.domain.Academician;
import com.fzhucc.modules.thinktank.domain.AcademicianMsgBox;
import com.fzhucc.modules.thinktank.domain.Capital;
import com.fzhucc.modules.thinktank.domain.CapitalMsgBox;
import com.fzhucc.modules.thinktank.domain.dto.*;
import com.fzhucc.modules.thinktank.repository.CapitalRepository;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.thinktank.repository.CapitalMsgBoxRepository;
import com.fzhucc.modules.thinktank.service.mapstruct.CapitalMsgBoxMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.sql.Timestamp;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2023-03-10
**/
@Service
@RequiredArgsConstructor
public class CapitalMsgBoxService {

    private final CapitalMsgBoxRepository capitalMsgBoxRepository;

    private final CapitalRepository capitalRepository;
    private final CapitalMsgBoxMapper capitalMsgBoxMapper;

    public Map<String,Object> queryAll(CapitalMsgBoxQueryCriteria criteria, Pageable pageable){
        Page<CapitalMsgBox> page = capitalMsgBoxRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Page<CapitalMsgBoxDto> map = page.map(capitalMsgBoxMapper::toDto);
        List<CapitalMsgBoxDto> list = map.getContent();
        if (list != null) {
            for (CapitalMsgBoxDto capitalMsgBoxDto : list) {
                Optional<Capital> res = capitalRepository.findById(capitalMsgBoxDto.getCapitalId());
                if (res.isPresent()) {
                    capitalMsgBoxDto.setCapital(res.get());
                }
            }
        }
        return PageUtil.toPage(map);
    }

    public List<CapitalMsgBoxDto> queryAll(CapitalMsgBoxQueryCriteria criteria){
        return capitalMsgBoxMapper.toDto(capitalMsgBoxRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    public CapitalMsgBoxDto findById(Integer id) {
        CapitalMsgBox capitalMsgBox = capitalMsgBoxRepository.findById(id).orElseGet(CapitalMsgBox::new);
        ValidationUtil.isNull(capitalMsgBox.getId(),"CapitalMsgBox","id",id);
        return capitalMsgBoxMapper.toDto(capitalMsgBox);
    }

    @Transactional(rollbackFor = Exception.class)
    public CapitalMsgBoxDto create(CapitalMsgBox resources) {
        return capitalMsgBoxMapper.toDto(capitalMsgBoxRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(CapitalMsgBox resources) {
        CapitalMsgBox capitalMsgBox = capitalMsgBoxRepository.findById(resources.getId()).orElseGet(CapitalMsgBox::new);
        ValidationUtil.isNull( capitalMsgBox.getId(),"CapitalMsgBox","id",resources.getId());
        capitalMsgBox.copy(resources);
        capitalMsgBoxRepository.save(capitalMsgBox);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            capitalMsgBoxRepository.deleteById(id);
        }
    }

    public void download(List<CapitalMsgBoxDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CapitalMsgBoxDto capitalMsgBox : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("产业资本id", capitalMsgBox.getCapitalId());
            map.put("咨询内容", capitalMsgBox.getContent());
            map.put("联系方式", capitalMsgBox.getPhone());
            map.put("咨询时间", capitalMsgBox.getConsultationTime());
            map.put("咨询人姓名", capitalMsgBox.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Transactional(rollbackFor = Exception.class)
    public CapitalMsgBoxDto seek(CapitalBoxSeekDto resources) {
        CapitalMsgBox capitalMsgBox = new CapitalMsgBox();
        Optional<Capital> res = capitalRepository.findById(resources.getCapitalId());
        if (!res.isPresent()) {
            throw new BadRequestException("未找到该产业资本");
        }
        capitalMsgBox.setCapitalId(resources.getCapitalId());
        capitalMsgBox.setPhone(resources.getPhone());
        capitalMsgBox.setConsultationTime(new Timestamp(new Date().getTime()));
        capitalMsgBox.setContent(resources.getContent());
        capitalMsgBox.setName(resources.getName());
        CapitalMsgBox capitalMsgBox1 = capitalMsgBoxRepository.save(capitalMsgBox);
        return capitalMsgBoxMapper.toDto(capitalMsgBox1);
    }
}